<?php
include 'imgresize.php';
include 'menu.php';
verifySession();
connectDB();


if($_POST["action"] == "Add machine")
{
	unset($imagename);
	if(!isset($_FILES) && isset($HTTP_POST_FILES))
		$_FILES = $HTTP_POST_FILES;
	if(!isset($_FILES['image_file']))
		$error["image_file"] = "An image was not found.";

	$imagename = basename($_FILES['image_file']['name']);
	//echo $imagename;
	if(empty($imagename))
		$error["imagename"] = "The name of the image was not found.";
	$thumbfile = "";
	$zoomfile = "";
	if(empty($error)) //picture exists, upload the pic as [cowtag].jpg and [cowtag]thumb.jpg
	{
		$newimage = "pictures/reconditioned/temp-import-pic.jpg";
		//echo $newimage;
		$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
		if(empty($result))
			$error["result"] = "There was an error moving the uploaded file.";

		$cowtag = ereg_replace("[^A-Za-z0-9]", "", $_POST["cowtag"]);

		$thumbfile = "pictures/reconditioned/" . $cowtag . "thumb.jpg";
		$thumb=new thumbnail($newimage);
		$thumb->size_height(60);
		$thumb->save($thumbfile);

		$zoomfile = "pictures/reconditioned/" . $cowtag . ".jpg";
		$zoom=new thumbnail($newimage);
		$zoom->size_auto(508);
		$zoom->save($zoomfile);

		unlink($newimage);
	}
	$sql="INSERT INTO `hydroclean`.`reconditioned` (`machineID`, `cowtag`, `category`, `description`, `price`, `piclink`, `thumblink`) "
		. "VALUES (NULL, '" . $_POST["cowtag"] . "', '" . $_POST["category"] . "', '" . $_POST["description"] . "', '" . $_POST["price"] . "','$zoomfile','$thumbfile');";
	mysql_query($sql);

	$setDate="INSERT INTO `hydroclean`.`updateLog` (`date`, `timeSpent`, `updatesMade`, `paidInd`, `recondUpdate`, `userID`) "
		. "VALUES ('" . date("Y-m-d") . "', '0', 'RECONDITIONED ADD', '0', '1', '" . $_SESSION['myusername'] . "');";


	mysql_query($setDate);
	

}
header("location:reconditioned_admin.php");

?>

